
package com.example.myapp_iphoneicon;

import com.example.demoex.R;

import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;

public class IphoneIconMainActivity extends Activity {
    private LayoutCalculator mLayoutCalculator;
    private Drawable mIconGloss;
    private Drawable mIconMask;
    private Drawable mIconShadow;
    private Paint mPaintResize;
    public static final int SHADOW_DOCKBAR_ICON = 2;
    public static final int SHADOW_NONE = 0;
    public static final int SHADOW_WALLPAPER_ICON = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iphone_icon_main);
        mLayoutCalculator = new LayoutCalculator(this);
        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
        ImageView imageView3 = (ImageView) findViewById(R.id.imageView3);
        mIconMask = getResources().getDrawable(R.drawable.appicon_mask);
        mIconShadow = getResources().getDrawable(R.drawable.appicon_shadow);
        mIconGloss = getResources().getDrawable(R.drawable.appicon_gloss);
        mPaintResize = new Paint();
        
        Drawable drawable = getResources().getDrawable(R.drawable.ic_icon_filemanager);
        
        int color = (getPackageCodePath().hashCode() ^ getPackageName().hashCode() ^ this.hashCode()) & 0xFFFFFF;
        Bitmap src = addIconWithBackground(color, ((BitmapDrawable)drawable).getBitmap(), null);
        
//        addIconWithBackground
        Rect bounds = new Rect(0, 0, src.getWidth(), src.getHeight());
        
        Bitmap bitmap = getIPhoneLikeIcon(src, bounds, true);
        src.recycle();
        Paint paintReflect = new Paint();// v7

        paintReflect.setAlpha(0x30);
        imageView3.setImageBitmap(drawIcon(mLayoutCalculator, new Canvas(bitmap), bitmap, 0, 500, paintReflect, SHADOW_WALLPAPER_ICON));
//        drawImageDropShadow(bitmap);
        imageView.setImageBitmap(drawImageDropShadow(bitmap, this));
        
        
        imageView2.setImageBitmap(createReflectedImage(drawImageDropShadow(bitmap, this)));
//        imageView.setImageBitmap(bitmap);
    }
    
    public void drawImageDropShadow(Bitmap bitmap){
        BlurMaskFilter blurFilter = new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER);
        Paint shadowPaint = new Paint();        
        shadowPaint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK); 
//        shadowPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
        shadowPaint.setMaskFilter(blurFilter);
        int[] offsetXY =new int[2];
        Bitmap originalBitmap = bitmap;//BitmapFactory.decodeResource(this.getResources(),R.drawable.favoritelist);
        Bitmap shadowBitmap = originalBitmap.extractAlpha(shadowPaint,offsetXY);
        
        Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas c = new Canvas(shadowImage32);       
        c.drawBitmap(originalBitmap, 0,0, null);
        ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
        imageView1.setImageBitmap(shadowImage32);    
    }
    
    public static Bitmap drawImageDropShadow(Bitmap originalBitmap,Context context) {
        
        BlurMaskFilter blurFilter = new BlurMaskFilter(3,
                        BlurMaskFilter.Blur.NORMAL);
        Paint shadowPaint = new Paint();
        shadowPaint.setAlpha(10);
         
 
        shadowPaint.setColor(context.getResources()
        .getColor(color.background_dark));
        shadowPaint.setMaskFilter(blurFilter);
         
        int[] offsetXY = new int[2];
        Bitmap shadowBitmap = originalBitmap
                        .extractAlpha(shadowPaint, offsetXY);

        Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas c = new Canvas(shadowImage32);
        c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);

        return shadowImage32;
}

    
    
    public Bitmap drawIcon(LayoutCalculator paramLayoutCalculator, Canvas paramCanvas, Bitmap localBitmapOne, int paramInt1,
            int paramInt2, Paint paramPaint, int paramInt3) {
//        if (localBitmapOne == null)
//            return;
//        Bitmap localBitmapTwo = mNotification;
        LayoutCalculator localLayoutCalculator = paramLayoutCalculator;
//        ObjectPool localObjectPool = paramObjectPool;
        Canvas localCanvas = paramCanvas;
        drawShadow(localLayoutCalculator,
                localCanvas, paramInt1, paramInt2, paramInt3);
        Paint localPaintOne = paramPaint;
        paramCanvas.drawBitmap(localBitmapOne, paramInt1, paramInt2,
                localPaintOne);
/*        if (localBitmapTwo == null)
            return;
        int marginRight = paramLayoutCalculator.mRcMarginRight;
        int left = paramLayoutCalculator.getIconWidth() + paramInt1
                - localBitmapTwo.getWidth() + marginRight;
        int marginTop = paramLayoutCalculator.mRcMarginTop;
        int top = paramInt2 - marginTop;
        Paint localPaintTwo = paramPaint;
        paramCanvas.drawBitmap(localBitmapTwo, left, top, localPaintTwo);*/
        
        return localBitmapOne;
    }
    //��Ӱ
    public static Bitmap createReflectedImage(Bitmap originalBitmap) {
        // ͼƬ�뵹Ӱ�������
        final int reflectionGap = 4;

        // ͼƬ�Ŀ��
        int width = originalBitmap.getWidth();
        // ͼƬ�ĸ߶�
        int height = originalBitmap.getHeight();

        Matrix matrix = new Matrix();
        // ͼƬ���ţ�x���Ϊԭ����1����y��Ϊ-1��,ʵ��ͼƬ�ķ�ת
        matrix.preScale(1, -1);
        // ������ת���ͼƬBitmap����ͼƬ����ԭͼ��һ�롣
        Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0,
                        height / 2, width, height / 2, matrix, false);
        // ������׼��Bitmap���󣬿��ԭͼһ�£�����ԭͼ��1.5����
        Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height
                        + height / 2 + reflectionGap), Config.ARGB_8888);

        // ���캯����Bitmap����Ϊ����ͼƬ�ϻ�ͼ
        Canvas canvas = new Canvas(withReflectionBitmap);
        // ��ԭʼͼƬ
        canvas.drawBitmap(originalBitmap, 0, 0, null);

        // ���������
        Paint defaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

        // ����ӰͼƬ
        canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

        // ʵ�ֵ�ӰЧ��
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,
                        originalBitmap.getHeight(), 0,
                        withReflectionBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
                        TileMode.MIRROR);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

        // ����Ч��
        canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(),
                        paint);

        return withReflectionBitmap;
}
    

    public void drawShadow(LayoutCalculator lc, Canvas cv,
            int left, int top, int shadowType) {
        Bitmap bitmapShadow;// v0
        switch (shadowType) {
        case SHADOW_WALLPAPER_ICON:
            bitmapShadow = BitmapManager.getWallpaperIconShadow(this);//op.getWallpaperIconShadow();
            int shadowLeft = left + (/*lc.mIconWidth*/140 - bitmapShadow.getWidth())
                    / 2;// v1
            int shadowTop = /*lc.mIconHeight*/140 + top - lc.dpToPixel(16)
                    - bitmapShadow.getHeight() / 2;// v2
            cv.drawBitmap(bitmapShadow, (float) shadowLeft, (float) shadowTop,
                    null);
            break;
        case SHADOW_DOCKBAR_ICON:
            bitmapShadow = BitmapManager.getDockbarIconShadow(this);//op.getDockbarIconShadow();
            cv.drawBitmap(bitmapShadow, (float) left, (float) top, null);
            break;
        }
    }
    

    private Bitmap getIPhoneLikeIcon(Bitmap paramBitmap, Rect paramRect,
            boolean paramBoolean) {
        Bitmap localBitmap = BitmapManager.createBitmap(
                mLayoutCalculator.mIconWidth, mLayoutCalculator.mIconHeight,
                Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap);
        RectF localRectF = new RectF(mLayoutCalculator.dpToPixel(0.67F), 0.0F,
                mLayoutCalculator.mIconWidth
                        - mLayoutCalculator.dpToPixel(1.33F),
                mLayoutCalculator.mIconHeight
                        - mLayoutCalculator.dpToPixel(2.0F));
        Paint localPaint = new Paint();
        localPaint.setFilterBitmap(true);
        localCanvas.drawBitmap(paramBitmap, paramRect, localRectF, localPaint);
        if (paramBoolean) {
            mIconGloss.setBounds(0, 0, mLayoutCalculator.mIconWidth,
                    mLayoutCalculator.mIconHeight);
            mIconGloss.draw(localCanvas);
        }
        Bitmap localBitmapSec = BitmapManager.createBitmap(
                mLayoutCalculator.mIconWidth, mLayoutCalculator.mIconHeight,
                Bitmap.Config.ARGB_8888);
        Canvas localCanvasSec = new Canvas(localBitmapSec);
        mIconMask.setBounds(0, 0, mLayoutCalculator.mIconWidth,
                mLayoutCalculator.mIconHeight);
        mIconMask.draw(localCanvasSec);
        Paint localPaintSec = new Paint();
        PorterDuff.Mode localMode = PorterDuff.Mode.SRC_IN;
        PorterDuffXfermode localPorterDuffXfermode = new PorterDuffXfermode(
                localMode);
        localPaintSec.setXfermode(localPorterDuffXfermode);
        localCanvasSec.drawBitmap(localBitmap, 0.0F, 0.0F, localPaintSec);
        localBitmap.recycle();
        Bitmap localBitmapTir = BitmapManager.createBitmap(
                mLayoutCalculator.mIconWidth, mLayoutCalculator.mIconHeight,
                Bitmap.Config.ARGB_8888);
        Canvas localCanvasTir = new Canvas(localBitmapTir);
        mIconShadow.setBounds(0, 0, mLayoutCalculator.mIconWidth,
                mLayoutCalculator.mIconHeight);
        mIconShadow.draw(localCanvasTir);
        localCanvasTir.drawBitmap(localBitmapSec, 0.0F, 0.0F, null);
        localBitmapSec.recycle();
        return localBitmapTir;
    }
    
    private Bitmap addIconWithBackground(int color, Bitmap src, Rect bounds) {

        int width = mLayoutCalculator.getIconWidth();
        int height = mLayoutCalculator.getIconHeight();
        Bitmap bitmap = BitmapManager.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(bitmap);
        cv.drawColor(Color.BLACK + color);

        cv.save();
        cv.clipRect(new Rect(0, height - (height * 2 / 5), mLayoutCalculator
                .getIconWidth(), height));

        float[] arrayOfFloat = new float[3];
        Color.colorToHSV(color, arrayOfFloat);
        arrayOfFloat[2] = arrayOfFloat[2] + 0.2F;
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setShader(new LinearGradient(width / 2, height, width / 2, height
                - (height * 2 / 5), Color.HSVToColor(0xff, arrayOfFloat),
                -16777216 + color, Shader.TileMode.MIRROR));
        cv.drawPaint(p);
        cv.restore();
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();

        if ((srcWidth > srcWidth) || (srcHeight > height)) {
            Rect rectDest = new Rect(0, 0, width, height);

            if (bounds == null) {
                bounds = new Rect(0, 0, src.getWidth(), src.getHeight());
            }

            cv.drawBitmap(src, bounds, rectDest, mPaintResize);
        } else {
            cv.drawBitmap(src, (width - srcWidth) / 2,
                    (height - srcHeight) / 2, null);
        }
        return bitmap;
    }

}
